package com.bjh.service.impl;

import com.bjh.dao.BusinessDao;
import com.bjh.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessDao businessDao;
    /**
     * 根据代理商id获取分组
     * @param params 相关参数
     * @return {@link List}
     * @throws Exception Exception
     */
    @Override
    public Map<String, Object> getGroupByAgentId(Map<String, Object> params) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("list", businessDao.getGroupByAgentId(params));
        resMap.put("count", businessDao.getGroupCountByAgentId(params));
        return resMap;
    }

    /**
     * 根据id查询分组详情
     * @param id 分组id
     * @return {@link Map}
     * @throws Exception Exception
     */
    @Override
    public Map<String, Object> getGroupById(Long id) throws Exception {
        return businessDao.getGroupById(id);
    }

    /**
     * 编辑分组
     * @param params 相关参数
     * @throws Exception Exception
     */
    @Override
    public void editGroup(Map<String, Object> params) throws Exception {
        params.put("parentId", "0");
        if(Objects.isNull(params.get("id"))){
            businessDao.addGroup(params);
        }else {
            businessDao.updateGroup(params);
        }
    }

    /**
     * 根据分组id获取商户数量
     * @param id 分组id
     * @return {@link Long}
     * @throws Exception Exception
     */
    @Override
    public Long getBusinessCountById(Long id) throws Exception {
        return businessDao.getBusinessCountById(id);
    }

    /**
     * 删除分组
     * @param id 分组id
     * @throws Exception Exception
     */
    @Override
    public void deleteGroup(Long id) throws Exception {
        Long count = businessDao.getBusinessCountById(id);
        if(count.intValue() == 0) businessDao.deleteGroup(id);
    }

    /**
     * 根据代理商id获取商户列表
     * @param params 相关参数
     * @return {@link Map}
     * @throws Exception Exception
     */
    @Override
    public Map<String, Object> getBusinessByAgentId(Map<String, Object> params) throws Exception {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("list", businessDao.getBusinessByAgentId(params));
        resMap.put("count", businessDao.getBusinessCountByAgentId(params));
        return resMap;
    }

    /**
     * 编辑商户
     * @param params 相关参数
     * @throws Exception Exception
     */
    @Override
    public void editBusiness(Map<String, Object> params) throws Exception {
        if(Objects.isNull(params.get("id"))){
            businessDao.addBusiness(params);
        }else {
            businessDao.updateBusiness(params);
        }
    }

    /**
     * 获取分组选项
     * @param agentId 代理商id
     * @return {@link List}
     * @throws Exception Exception
     */
    @Override
    public List<?> getGroupOption(Long agentId) throws Exception {
        return businessDao.getGroupOption(agentId);
    }
}
